package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.repository.MonitorFaultRepository;
import com.hualude.roadsign.repository.OrgRepository;
import com.hualude.roadsign.repository.PushHistoryRepository;
import com.hualude.roadsign.repository.UserRepository;
import com.hualude.roadsign.service.MonitorFaultCheckService;
import com.hualude.roadsign.utils.PushUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by ding on 2017/8/7.
 */
@Service
public class MonitorFaultCheckServiceImpl implements MonitorFaultCheckService {

    @Resource
    private MonitorFaultRepository monitorFaultRepository;
    @Resource
    private UserRepository userRepository;
    @Resource
    private OrgRepository orgRepository;
    @Resource
    private PushHistoryRepository pushHistoryRepository;

    private String strUserIds;

    private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public DTResponseDto getMonitorFaultCheck4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId) throws Exception {
        List<DTMonitorFaultDto> dto4DTs = monitorFaultRepository.getMonitorFaultCheck4DT(dtRequestDto, actorId, orgId);
        Integer count = monitorFaultRepository.getMonitorFaultCheckCount4DT(dtRequestDto, actorId, orgId);
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, dto4DTs);
        for (DTMonitorFaultDto dtMonitorFaultDto : dto4DTs) {
            String creatorName = userRepository.getUserNameByUserId(dtMonitorFaultDto.getMonitorFaultCreatorId());
            dtMonitorFaultDto.setMonitorFaultCreatorName(creatorName);
            String checkerName = userRepository.getUserNameByUserId(dtMonitorFaultDto.getMonitorCheckerId());
            dtMonitorFaultDto.setMonitorCheckerName(checkerName);
            if (dtMonitorFaultDto.getMonitorMainCheckerId() != null) {
                String mainCheckerName = userRepository.getUserNameByUserId(dtMonitorFaultDto.getMonitorMainCheckerId());
                dtMonitorFaultDto.setMonitorMainCheckerName(mainCheckerName);
            }
        }
        return dtResponseDto;
    }


    @Transactional
    public Integer monitorFaultAudit(Integer isPass, String feedback, Integer lampFaultId, Integer userId) throws Exception {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(d);
        Integer check = monitorFaultRepository.getLampCheck(lampFaultId);
        //check:1,主管在审核
        check = (isPass == 2) ? 1 : check;
        if (check == 1) {
            if (isPass == 1 || isPass == 2) {
                final String[] strFaultType = {"损毁", "其他"};
                final MonitorFaultDto faultDto = monitorFaultRepository.getInfoByFaultId(lampFaultId);
                final Integer orgId = faultDto.getMonitorOrgId();
                List<UserDto> userDtos = userRepository.getUserIdsByOrgIdAndActorId(orgId, 5);
                final String msg = "监控箱 NO：" + faultDto.getMonitorId() + " " +
                        strFaultType[faultDto.getMonitorFaultType()];
                strUserIds = "";
                for (UserDto userDto : userDtos) {
                    strUserIds += userDto.getUserId().toString() + ",";
                }
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (!strUserIds.equals("")) {
                                strUserIds = strUserIds.substring(0, strUserIds.length() - 1);
                                PushUtil.sendPushToUser(strUserIds, msg);
                                PushHistoryDto pushHistoryDto = new PushHistoryDto(orgId, 5, msg, formatter.format(new Date()),8);
                                pushHistoryRepository.save(pushHistoryDto.toBean());
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                if (isPass == 1)
                    return monitorFaultRepository.faultAudit2Success(isPass, feedback, lampFaultId, userId, dateNowStr);
                else
                    return monitorFaultRepository.straightFaultAudit(isPass, feedback, lampFaultId, userId, dateNowStr);
            } else {
                return monitorFaultRepository.faultAudit2Fail(isPass, feedback, lampFaultId, userId, dateNowStr);
            }
        } else {
            if (isPass == 1) {
                final MonitorFaultDto faultDto = monitorFaultRepository.getInfoByFaultId(lampFaultId);
                List<UserDto> userDtos = userRepository.getUserIdsByOrgIdAndActorId(faultDto.getMonitorMainOrgId(), 7);
                final String msg = "监控箱 NO：" + faultDto.getMonitorId() + " 待审核";
                strUserIds = "";
                for (UserDto userDto : userDtos) {
                    strUserIds += userDto.getUserId().toString() + ",";
                }
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (!strUserIds.equals("")) {
                                strUserIds = strUserIds.substring(0, strUserIds.length() - 1);
                                PushUtil.sendPushToUser(strUserIds, msg);
                                PushHistoryDto pushHistoryDto = new PushHistoryDto(faultDto.getMonitorMainOrgId(), 7, msg, formatter.format(new Date()),8);
                                pushHistoryRepository.save(pushHistoryDto.toBean());
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
            return monitorFaultRepository.faultAudit(isPass, feedback, lampFaultId, userId, dateNowStr);
        }
    }

    public List<MonitorFaultDto> getMonitorFaultCheckList4App(Integer orgId, String actorIds, Integer faultStatus, Integer count, Integer size) throws Exception {
        return monitorFaultRepository.getMonitorFaultCheckList4App(orgId, actorIds, faultStatus, count, size);
    }

    public MonitorFaultDto getMonitorInfo2Fault(Integer monitorId) throws Exception {
        MonitorFaultDto monitorFaultDto = monitorFaultRepository.getMonitorInfo2Fault(monitorId);
        monitorFaultDto.setMonitorCreatorName(userRepository.getUserNameByUserId(monitorFaultDto.getMonitorCreatorId()));
        monitorFaultDto.setMonitorOrgName(orgRepository.getMainOrgName(monitorFaultDto.getMonitorOrgId()));
        monitorFaultDto.setMonitorMainOrgName(orgRepository.getMainOrgName(monitorFaultDto.getMonitorMainOrgId()));
        monitorFaultDto.setMonitorId(monitorId);
        return monitorFaultDto;
    }

    public MonitorFaultDto getMonitorFaultInfo2Fault(Integer monitorId, Integer monitorFaultId) throws Exception {
        MonitorFaultDto signalLampFaultDto = monitorFaultRepository.getMonitorInfo2Fault(monitorId);
        MonitorFaultDto monitorFaultDto = monitorFaultRepository.getSignalLampFaultInfo2Fault(monitorFaultId);
        monitorFaultDto.setMonitorProvince(signalLampFaultDto.getMonitorProvince());
        monitorFaultDto.setMonitorCity(signalLampFaultDto.getMonitorCity());
        monitorFaultDto.setMonitorArea(signalLampFaultDto.getMonitorArea());
        monitorFaultDto.setMonitorStartRoad(signalLampFaultDto.getMonitorStartRoad());
        monitorFaultDto.setMonitorEndRoad(signalLampFaultDto.getMonitorEndRoad());
        monitorFaultDto.setMonitorRoadName(signalLampFaultDto.getMonitorRoadName());
        monitorFaultDto.setMonitorLongitude(signalLampFaultDto.getMonitorLongitude());
        monitorFaultDto.setMonitorLatitude(signalLampFaultDto.getMonitorLatitude());
        monitorFaultDto.setMonitorOrgId(signalLampFaultDto.getMonitorOrgId());
        monitorFaultDto.setMonitorMainOrgId(signalLampFaultDto.getMonitorMainOrgId());
        UserDto userDto = userRepository.getUserById(monitorFaultDto.getMonitorFaultCreatorId());
        monitorFaultDto.setMonitorFaultCreatorName(userDto.getUserName());
        monitorFaultDto.setMonitorFaultCreatorPhone(userDto.getUserMobile());
        return monitorFaultDto;
    }

    public List<MonitorFaultDto> getMonitorFaultReportList4App(Integer orgId, String actorIds, Integer faultStatus, Integer count, Integer size) throws Exception {
        return monitorFaultRepository.getMonitorFaultReportList4App(orgId, actorIds, faultStatus, count, size);
    }
}
